<!--#include file="./load.inc"-->
<%
call app.checkPost()
dim aq, data, iid, cid, replyID, editTitle, checkCode, codeUrl, isShenHe, addrUrl, checkTimes
dim book, bookLen, bookUnit, bookName
bookLen = app.getConfig("bookLen")
bookUnit = app.getGenreInfo(2)
bookName = bookUnit(3)
if not(isBook) then app.message "未开放" & bookName & "功能！"
if bookAuditing then isShenHe = "不"
isShenHe = "<font color=""#666666"">(" & isShenHe & "须审核)</font>&nbsp;"
addrUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip="
checkTimes = 30 '再次发帖或回复需要等待的时间：秒

aq = lcase(request("q"))
iid = app.int(request("id"))
checkCode = app.int(request("checkCode"))
editCode = app.int(request("editCode"))

codeUrl = siteUrl & "code.asp"
editTitle = "添加"

set book = new bookClass

select case aq
	case "save"		    :		call book.bookSave() '保存新发表、回复、回复回复的
	case "edit"		    :		call book.bookEdit() '修改
	case "getclass" 	:		call book.getGenreClass() '取得分类
	case else	        :		call book.bookAdd() '默认：新发表、回复、回复回复的
end select
call db.close()

class bookClass
	public function bookEdit()
	    dim rs, sql, str
		if iid > 0 then
			replyID = app.int(request("re"))
			if replyID = 1 or replyID = 2 then
				editTitle = "修改"
				testCheck()
				if editCode = 0 then
					app.message "请输入" & bookName & "编辑码！"
				else
					if len(trim(editCode)) < 4 or len(trim(editCode)) > 8 then app.message bookName & "编辑码只能为4-8位数字！"
				end if
			end if
			dim mailSend, mailDomain, mailUserName, mailPassword, mailFrom, bookBanWord
			mailSend = app.getConfig("mailSend")
			mailDomain = app.getConfig("mailDomain")
			mailUserName = app.getConfig("mailUserName")
			mailPassword = app.getConfig("mailPassword")
			mailFrom = app.getConfig("mailFrom")
			bookAuditing = app.getConfig("bookBanWord")
			select case replyID
				case 1 '修改
					checkTime()
					sql = "select title,userName,userEmail,dateTime,content from cj_book where isShow=true and auditing=true and id=" & iid & " and editCode=" & editCode
					set rs = db.exec(sql, 1)
					if rs.eof then message "错误原因：1：id不存在；2：未审核" & bookName & "；3：非公开" & bookName & "。"
					if dateDiff("d", rs("dateTime"), now()) > 30 then message bookName & "已超过30天不可以修改！"
					str = "<fieldset><legend>" & editTitle & bookName & "</legend><form action=""" & fileName & """ method=""post"">"
					str = str & "<input type=""hidden"" value=""2"" name=""q"" />"
					str = str & "<input type=""hidden"" value=""2"" name=""re"" />"
					str = str & "<input type=""hidden"" value=""" & iid & """ name=""id"" />"
					str = str & "<input type=""hidden"" value=""" & editCode & """ name=""editCode"" />"
					str = str & "<p>提示：" & bookName & "发表后30天后不可修改！</p>"
					str = str & "<p>标&nbsp;&nbsp;题：<input data-title=""请输入标题，不能包含特殊字符，字数在5-100字"" name=""title"" class="""
					str = str & "txt inp"" value=""" & app.hec(rs("title")) & """ style=""width:50%;"" /><b class=""red"">*</b></p>"
					str = str & "<p>" & bookName & "者：<input data-title=""请输入你的大名，字数在2-30字"" name=""userName"" class=""txt inp"" value="""
					str = str & app.hec(rs("userName")) & """ style=""width:100px;"" /><b class=""red"">*</b>，"
					str = str & "E-mail：<input data-title=""找回“编辑码”和管理员与你联系之用,不公开。可留空"" name=""userEmail"""
					str = str & " class=""txt inp"" value=""" & app.hec(rs("userEmail")) & """ />，"
					str = str & "验证码：<input data-title=""随机验证码，防止机器人提交！"" name=""checkCode"" class=""txt inp"""
					str = str & " style=""width:60px;"" maxlength=""" & checkAmount & """ /><b class=""red"">*</b>" & codeUrl
					str = str & "，<span data-title=""是否公开留言，如不公开，只有管理员看到，也不可再修改！"">"
					str = str & "<input name=""isShow"" type=""checkbox"" checked /> 是否公开</span>"
					str = str & "</p><p>内容(10-" & bookLen & "字[含HTML代码])：(已输入 <span class=""red word_count"">0</span> 字)<br /><textarea name=""content"" class=""ate"" style=""height:300px;width:100%;"">" & app.hec(ReBookUrl(rs("content")))
					str = str & "</textarea></p><center>"
					str = str & "<input type=""button"" class=""but"" onClick=""history.back(-1)"" value=""&nbsp;返&nbsp;回&nbsp;"" /> "
					str = str & "<button type=""submit"" data-title=""请遵守国家相关法律法规，文明发言！"">&nbsp;确&nbsp;定&nbsp;"
					str = str & isShenHe
					str = str & "</button></center></form></fieldset>"
					str = str & loadEdit("content")
					set rs = nothing
					call html(editTitle & bookName, str)
				case 2 '保存修改 需要输入验证码
					checkTime()
					title = app.reChar(request("title"))
					userName = app.reChar(request("userName"))
					userEmail = app.reChar(request("userEmail"))
					content = trim(request("content"))
					userName = testName(userName)
					userName = testWord(userName)
					if len(userName) < 2 or len(userName) > 30 then message "名字字数在2-30字！"
					if len(title) < 5 or len(title) > 100 then message "标题字数在5-100字！"
					content = testCN(content)
					content = testWord(content)
					content = formatBook(content)
					if len(content) < 10 or len(content) > bookLen then message "内容字数在10-" & bookLen & "字！"
					userEmail = testEmail(userEmail)
					sql = "select title,classID,userName,userEmail,dateTime,content,replyID from cj_book where isShow=true and auditing=true and id=" & iid & " and editCode=" & editCode
					set rs = db.exec(sql, 3)
					if rs.eof or rs.bof then
						rs.close : set rs = nothing
						message "错误原因：1：id不存在；2：未审核" & bookName & "；3：非公开" & bookName & "。"
					end if
					if dateDiff("d", rs("dateTime"), now()) > 30 then
						rs.close : set rs = nothing
						message bookName & "已超过30天不可以修改！"
					end if
					rs("title") = title
					rs("userName") = userName
					rs("content") = content
					rs("userEmail") = userEmail
					cid = rs("classID")
					iid = rs("replyID")
					rs.update
					rs.close
					set rs = nothing
					if userEmail <> "" then app.setCookie "userEmail", userEmail
					app.setCookie "userName", userName
					app.setCookie "editCode", editCode
					session(cacheName) = ""
					session("bookStop") = now()
					str = "修改成功！<br /><button onclick=""top.location.href='" & app.getTypeUrl("l", cid) & "'"">&nbsp;返回" & bookName& "列表&nbsp;</button>，"
					str = str & "<button onclick=""top.location.href='" & app.getInfoUrl(2, iid, 1) & "'"">&nbsp;返回该主题&nbsp;</button>"
					app.message str
				case 3 '找回编辑码
					str = "<fieldset><legend>输入当时" & bookName & "预留的邮箱</legend>"
					str = str & "<form action=""" & fileName & """ method=""post"">"
					str = str & "<input type=""hidden"" value=""2"" name=""q"" />"
					str = str & "<input type=""hidden"" value=""4"" name=""re"" />"
					str = str & "<input type=""hidden"" value=""" & iid & """ name=""id"" />"
					str = str & "<p>提示：" & bookName & "发表后30天后不可修改！</p>"
					str = str & "<p>邮箱：<input data-title=""输入当时" & bookName & "预留的邮箱！"""
					str = str & " name=""userEmail"" class=""txt inp"" value=""" & app.getCookie("userEmail") & """ /><b class=""red"">*</b>"
					str = str & "，验证码：<input data-title=""随机验证码，防止机器人提交！"" name=""checkCode"" class=""txt inp"""
					str = str & " style=""width:60px;"" maxlength=""" & checkAmount & """ /><b class=""red"">*</b>" & codeUrl
					str = str & " <button type=""submit"">&nbsp;确&nbsp;定&nbsp;</button></p>"
					str = str & "<p>如果连邮箱都忘记了，那只有留言告知管理员修改了。</p>"
					str = str & "</form></fieldset>"
					call html("输入当时" & bookName & "预留的邮箱", str)
				case 4 '邮箱找回编辑码
					testCheck()
					checkTime()
					sql = "select userName,editCode,userEmail,dateTime from cj_book where isShow=true and auditing=true and editCode>0 and id=" & iid
					set rs = db.exec(sql, 1)
					if rs.eof then message "错误原因：1：id不存在；2：未审核" & bookName & "；3：非公开" & bookName & "；4：未预留有编辑码。"
					dateTime = rs("dateTime")
					userEmail = "" & rs("userEmail")
					userName = rs("userName")
					editCode = rs("editCode")
					set rs = nothing
					if dateDiff("d", dateTime, now()) > 30 then message bookName & "已超过30天不可以修改！"
					if userEmail = "" then message "没有预留邮箱！"
					app.setCookie "userEmail", userEmail
					app.setCookie "userName", userName
					app.setCookie "editCode", editCode
					dim msg, obj
					msg = "<h2 style=""color:red;"">" & userName & "：</h2>"
					msg = msg & "<p>这封信是由 <a target=""_blank"" href=""" & siteUrl & """>" & siteName & "</a> 发送的，请勿回复！</p>"
					msg = msg & "<h3>" & bookName & "编辑码为：" & editCode & "，编辑的截至有效期为" & DateAdd("d", 30, dateTime) & "！</h3>"
					'sendEMail(iMailTitle, iMailBody, iMailAddr, iMailSend, iMailDomain, iMailUserName, iMailPassword, iMailFrom, iMailFromName)
					str = sendEMail(userName & "：【" & siteName & "】邮箱找回" & bookName & "编辑码", msg, userEmail, mailSend, mailDomain, mailUserName, mailPassword, mailFrom, siteName)
					set obj = nothing
					session(cacheName) = ""
					session("bookStop") = now() '防止刷新重复提交
					app.message str
				case else '修改前需要输入验证码
					str = "<fieldset><legend>输入当时" & bookName & "预留的编辑码</legend>"
					str = str & "<form action=""" & fileName & """ method=""post"">"
					str = str & "<input type=""hidden"" value=""2"" name=""q"" />"
					str = str & "<input type=""hidden"" value=""1"" name=""re"" />"
					str = str & "<input type=""hidden"" value=""" & iid & """ name=""id"" />"
					str = str & "<p>提示：" & bookName & "发表后30天后不可修改！</p>"
					str = str & "<p>编辑码：<input data-title=""编辑码为4-8位数字，不能以0开头。留空或为0则不可再编辑！输入后不可再修改！"""
					str = str & " name=""editCode"" class=""txt inp"" maxlength=""8"" value=""" & app.getCookie("editCode") & """ style=""width:60px;"" /><b class=""red"">*</b>"
					str = str & "，验证码：<input data-title=""随机验证码，防止机器人提交！"" name=""checkCode"" class=""txt inp"""
					str = str & " style=""width:60px;"" maxlength=""" & checkAmount & """ /><b class=""red"">*</b>" & codeUrl
					str = str & " <button type=""submit"">&nbsp;确&nbsp;定&nbsp;</button></p>"
					str = str & "</form></fieldset>"
					call html("输入当时预留的编辑码", str)
			end select
		else
			response.redirect "http://" & webHost & fileName
		end if
	end function

	'默认：新发表、回复、回复回复的
	public function bookAdd()
		checkTime()
		userName = app.getCookie("userName")
		if userName = "" then
			if app.isAdmin then userName = siteName
		end if
		dim rs, sql
		data = array(app.int(request("classID")), userName, "", "")
		if iid > 0 then
			sql = "select classID,userName,title,content,dateTime from cj_book where isShow=true and auditing=true and id=" & iid
			set rs = db.exec(sql, 1)
			if rs.eof or rs.bof then
			    app.message "错误原因：1：id不存在；2：未审核" & bookName & "；3：非公开" & bookName & "。"
			end if
			editTitle = "回复"
			data(2) = "re " & rs("title")
			data(3) = "<blockquote>" & rs("userName") & " 发表于 " & rs("dateTime") & "<br>" & left(app.noCode(rs("content")), 50) & "...</blockquote><p>&nbsp;</p>"
			set rs = nothing
		end if
		%><!--#include file="./view/web/book.add.asp"--><%
	end function

	public function bookSave()
		testCheck()
		checkTime()
		if editCode > 0 then
			if len("" & editCode) < 4 or len("" & editCode) > 8 then app.message bookName & "编辑码只能为4-8位数字！"
		end if
		title = app.reChar(request("title"))
		userName = app.reChar(request("userName"))
		userEmail = app.reChar(request("userEmail"))
		content = trim(request("content"))
		userName = testName(userName)
		userName = testWord(userName)
		if len(userName) < 2 or len(userName) > 30 then app.message "名字字数在2-30字！"
		if len(title) < 5 or len(title) > 100 then app.message "标题字数在5-100字！"
		content = testCN(content)
		content = testWord(content)
		content = formatBook(content)
		if len(content) < 10 or len(content) > bookLen then app.message "内容字数在10-" & bookLen & "字！"
		userEmail = testEmail(userEmail)
		userAddr = GetAddr()
		if app.isAdmin then '如果是管理员
			userID = adminInfo(0)
			userName = siteName
		end if
		dim rs, sql, str
		if iid > 0 then '回复主题、回复回复的
			editTitle = "回复"
			sql = "select replyID,classID from cj_book where isShow=true and auditing=true and id=" & iid
			set rs = db.exec(sql, 1)
			if rs.eof or rs.bof then app.message "错误原因：1：id不存在；2：未审核" & bookName & "；3：非公开" & bookName & "。"
			cid = rs("classID")
			replyID = app.int(rs("replyID"))
			set rs = nothing
			'replyID默认是回复的主题的replyID，如果是主题replyID=0，replyID就是主题Iid
			if replyID = 0 then replyID = iid
		else
			cid = app.int(request("classID"))
			if inBookClassID(cid) then app.message bookName & "分类不存在！"
		end if
		
		set rs = db.exec("select * from cj_book", 3)
		rs.AddNew
		rs("classID") = cid
		rs("title") = title
		rs("ReAdmin") = userID
		rs("userName") = userName
		rs("userIP") = userIP
		rs("userAddr") = userAddr
		rs("content") = content
		rs("userEmail") = userEmail
		rs("auditing") = bookAuditing
		rs("replyID") = replyID
		rs("editCode") = editCode
		
		rs.update
		rs.close
		set rs = nothing
		if iid = 0 then
			iid = conn.execute("select max(id) cj_book")(0)
		end if
		if userEmail <> "" then app.setCookie "userEmail", userEmail
		if editCode > 0 then app.setCookie "editCode", editCode
		app.setCookie "userName", userName
		session(cacheName) = ""
		session("bookStop") = now()
		str = editTitle & "成功！<br /><button onclick=""top.location.href='" & app.getTypeUrl("l", cid) & "'"">&nbsp;返回" & bookName& "列表&nbsp;</button>，"
		str = str & "<button onclick=""top.location.href='" & app.getInfoUrl(2, iid, 1) & "'"">&nbsp;返回该主题&nbsp;</button>"
		app.message str
	end function

	public function html(ti, ByVal val)
		dim str
		str = "<!doctype html><html><head><meta charset=""utf-8"" /><title>" & ti & "</title>"
		str = str & "<link href=""" & siteUrl & "view/style/style2/style.css"" rel=""stylesheet"" />"
		str = str & "<script src=""" & siteUrl & "view/style/js/jquery-1.9.1.min.js""></script>"
		str = str & "<script src=""" & siteUrl & "view/style/js/common.js""></script>"
		str = str & "</head><body><div class=""bookedit"" style=""margin:10px auto;"">" & val & "</div></body></html>"
		response.write str
	end function

	public function loadEdit(id)
		dim str
		str = "<link rel=""stylesheet"" href=""" & siteUrl & "view/style/kindeditor/themes/default/default.css"" />"
		'str = str & "<link rel=""stylesheet"" href=""" & siteUrl & "view/style/kindeditor/themes/simple/simple.css"" />"
		str = str & "<script src=""" & siteUrl & "view/style/kindeditor/kindeditor-min.js""></script>"'
		str = str & "<script src=""" & siteUrl & "view/style/kindeditor/lang/zh_CN.js""></script>"
		str = str & "<script>loadEdit('textarea[name=""" & id & """]');</script>"
		loadEdit = str
	end function

	public function classHtml(ByVal id)
		dim gid, str, i, j
		gid = app.getGenreClass(2)
		id = trim(id)
		if isArray(gid) then
			str = "<select name=""classID"">"
			for each i in gid
				str = str & "<option value=""" & i & """"
				if trim(i) = id then str = str & " selected"
				str = str & ">" & app.getClassInfo(i)(2) & "</option>"
			next
		str = str & "</select>"
		end if
		classHtml = str
	end function

	public function GetHtml(ByVal URL, ByVal Cset)
		on error resume next
		if len(Cset) < 2 then Cset = "GB2312"
		dim strHeader, l, obj, objStream
		set objStream = server.createObject("ADODB.Stream")
		objStream.type = 1
		objStream.mode = 3
		objStream.open
		set obj = server.createObject("MSXML2.XMLHTTP")
		obj.open "GET", URL, false
		obj.setrequestHeader "Referer", URL
		obj.send
		if obj.readyState <> 4 then exit function
		if obj.Status > 300 then exit function
		'--获取目标网站文件头
		strHeader = obj.getresponseHeader("content-Type")
		strHeader = ucase(strHeader)
		objStream.write (obj.responseBody)
		set obj = nothing
		
		if len(strHeader) > 0 then
			'--获取目标文件编码
			l = inStrRev(strHeader, "CHARSET=", -1, 1)
			if l > 0 then
				Cset = right(strHeader, len(strHeader) - l - 7)
			else
				Cset = Cset
			end if
		end if
		objStream.position = 0
		objStream.type = 2
		objStream.charset = trim(Cset)
		GetHtml = objStream.ReadText
		objStream.close
		set objStream = nothing
	end function

	'取得大类下的栏目分类
    public function getGenreClass()
    	dim arr, j, k, data()
    	arr = app.getGenreClass(2)
    	if isArray(arr) then
    	    k = 0
    		for each i in arr
    			j = app.getClassInfo(i)
    			if isArray(j) then
    				redim preserve data(k)
    				data(k) = j
    				k = k + 1
    			end if
    		next
    		app.json data, 0
    	end if
    	app.json "没有数据", 1
    end function

	public function GetAddr()
		dim i, ii, Addr
		Addr = app.getCookie("userAddr")
		if Addr = "" or Addr = "未知" then
			if userIP <> "" and userIP <> "127.0.0.1" and userIP <> "localhost" then
				Addr = GetHtml(addrUrl & userIP, "gbk")
				if left(Addr, 1) <> "-" and len(Addr) > 2 then
					re.pattern = "\d+\s+\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s+\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s+(.+)"
					set ii = re.execute(Addr)
					for each i in ii
						Addr = i.subMatches(0)
					next
					set ii = nothing
					Addr = replace(replace(replace(Addr, "	", " "), "  ", " "), "中国 ", "")
					app.setCookie "userAddr", Addr
				else
					Addr = "未知"
				end if
			else
				Addr = "本机"
			end if
		end if
		GetAddr = Addr
	end function

	'检测留言分类是否存在，存在返回false，不存在则返回true
	public function inBookClassID(ByVal id)
		inBookClassID = true
		id = trim(id)
		if id = 0 then exit function
		dim i, j
		j = app.getGenreClass(2)
		if isArray(j) then
			for each i in j
				if trim(i) = id then
					inBookClassID = false
					exit function
				end if
			next
		end if
	end function

	public function testCheck()
		if checkCode = 0 then app.message "请输入验证码！"
		if cstr(checkCode) <> session(cacheName) then app.message "验证码错误，请重新输入验证码！"
	end function

	public function checkTime()
		if checkTimes > 0 then
			if dateDiff("s", session("bookStop"), now) < checkTimes then
                app.message "操作时间间隔需" & checkTimes & "秒！"
			end if
		end if
	end function

	public function testEmail(ByVal str)
		str = trim(str)
		if len(str) > 5 then
			str = app.quanToBan(str)
			re.pattern = "^([\w\-.]+@[\w\-]+\.[a-z]{2,4})+$"
			if not(re.test(str)) then app.message "邮箱格式错误！应为：x@x.x。如果没有请留空！"
		else
			str = ""
		end if
		testEmail = str
	end function

	public function testCN(ByVal str)
		str = trim(str)
		if len(str) = 0 then
			app.message "内容不可以为空！"
			exit function
		end if
		re.pattern = "^[^\u4E00-\u9FA5]+$"
		if re.test(str) then app.message "内容必须包含中文！"
		testCN = str
	end function

	public function testName(ByVal str)
		str = trim(str)
		if len(str) = 0 then
			app.message "名字不可以为空！"
			exit function
		end if
		str = app.quanToBan(str)
		re.pattern = "(管.*理.*[员|者]*.*)|admim"
		if re.test(str) then app.message "名字不可以包含“管理员”、“管理者”、“admin”等字符！"
		testName = str
	end function

	public function testWord(ByVal str)
		on error resume next
		str = trim(str)
		if bookBanWord = "" then
			testWord = str
			exit function
		end if
		dim i, j, k
		j = split(app.getConfig("bookBanWord"), "$$$")
		for i = 0 to ubound(j)
			k = j(i)
			if k <> "" then
				re.pattern = k
				if re.test(str) then app.message "内容包含了非法或敏感字符！"
			end if
		next
		testWord = str
	end function

	public function formatBook(ByVal str)
		str = trim(str)
		if len(str) < 1 then exit function
		dim i, ii, n, nn, m, mm, x, y, j, k, tag1, tag2, data()
		tag1 = "【WWW_CJASP_COM_KEYWORD_"
		tag2 = "_WWW_CJASP_COM_KEYWORD】"
		j = 0
		str = app.quanToBan(str)
		'把TAG过滤掉
		re.pattern = tag1 & "|" & tag2 & "|	|" & chr(13)
		str = re.replace(str, "")
		
		'去掉标记前后的空白
		re.pattern = "\s+<\s*"
		str = re.replace(str, "<")
		re.pattern = "\s*>\s+"
		str = re.replace(str, ">")
		
		'注释
		re.pattern = "<\!--[\S\s]*?-->"
		str = re.replace(str, "")

		re.pattern = "<\?[\S\s]*?\?>|<\%[\S\s]*?\%\>|<\![^>]*>"
		str = re.replace(str, "")
		
		re.pattern = "<(abbr|acronym|address|applet|article|aside|audio|bdi|bdo|big|body|button|canvas|caption|center|cite|code|col|colgroup|command|datalist|dd|del|details|dfn|dir|dl|dt|fieldset|figcaption|figure|font|footer|form|frame|frameset|head|header|hgroup|html|iframe|input|ins|kbd|keygen|label|legend|link|map|mark|menu|meta|meter|nav|noframes|noscript|object|optgroup|option|output|param|pre|progress|q|rp|rt|ruby|samp|script|select|small|source|strike|style|summary|table|tbody|td|textarea|tfoot|th|thead|time|title|tr|track|tt|var|video|wbr)[^>]*>[\S\s]*?<\/\1>"
		str = re.replace(str, "")
		
		re.pattern = "<(area|base|basefont|embed|abbr|acronym|address|applet|article|aside|audio|bdi|bdo|big|body|button|canvas|caption|center|cite|code|col|colgroup|command|datalist|dd|del|details|dfn|dir|dl|dt|fieldset|figcaption|figure|font|footer|form|frame|frameset|head|header|hgroup|html|iframe|input|ins|kbd|keygen|label|legend|link|map|mark|menu|meta|meter|nav|noframes|noscript|object|optgroup|option|output|param|pre|progress|q|rp|rt|ruby|samp|script|select|small|source|strike|style|summary|table|tbody|td|textarea|tfoot|th|thead|time|title|tr|track|tt|var|video|wbr)[^>]*>"
		str = re.replace(str, "")
		
		'处理a链接
		re.pattern = "<a\s+([^>]+)>(.+?)<\/a>"
		set ii = re.execute(str)
		for each i in ii
			x = ""
			re.pattern = "href\s*=\s*""(.+)"""
			set nn = re.execute(i.subMatches(0))
			for each n in nn
				x = replace(n.subMatches(0), "'", "")
				exit for
			next
			if x <> "" then
				redim preserve data(j)
				data(j) = "<span url=""" & x & """ class=""book_url cur"">" & noHtml(i.subMatches(1)) & "</span>"
				str = replace(str, i, tag1 & j & tag2)
				j = j + 1
			else
				str = replace(str, i, "")
			end if
		next
		
		'处理hr|br
		re.pattern = "<(hr|br)[\s\/]*>"
		set ii = re.execute(str)
		for each i in ii
			redim preserve data(j)
			data(j) = "<" & i.subMatches(0) & " />"
			str = replace(str, i, tag1 & j & tag2)
			j = j + 1
		next
		
		'处理strong
		re.pattern = "<strong>([\S\s]*?)<\/strong>"
		set ii = re.execute(str)
		for each i in ii
			x = noHtml(i.subMatches(0))
			if x <> "" then
				redim preserve data(j)
				data(j) = "<strong>" & x & "</strong>"
				str = replace(str, i, tag1 & j & tag2)
				j = j + 1
			else
				str = replace(str, i, "")
			end if
		next
		'处理blockquote
		re.pattern = "<blockquote>([\S\s]*?)<\/blockquote>"
		set ii = re.execute(str)
		for each i in ii
			x = noHtml(i.subMatches(0))
			re.pattern = "<\/*(?!br)[^>]*>"
			x = re.replace(x, "")
			if x <> "" then
				redim preserve data(j)
				data(j) = "<blockquote>" & x & "</blockquote>"
				str = replace(str, i, tag1 & j & tag2)
				j = j + 1
			else
				str = replace(str, i, "")
			end if
		next
		
		'处理img
		re.pattern = "<img\s+([^>]+)>"
		set ii = re.execute(str)
		for each i in ii
			x = ""
			k = i.subMatches(0)
			if k <> "" then
				re.pattern = "(src|width|height|alt|align)\s*=""(.+?)"""
				set nn = re.execute(k)
				for each n in nn
					x = x & " " & n.subMatches(0) & "=""" & n.subMatches(1) & """"
				next
			end if
			if x <> "" and inStr(x, " src=""") > 0 then
				redim preserve data(j)
				data(j) = "<img" & x & " />"
				str = replace(str, i, tag1 & j & tag2)
				j = j + 1
			else
				str = replace(str, i, "")
			end if
		next
		
		'处理span
		re.pattern = "<span\s+([^>]+)>([\S\s]+?)<\/span>"
		set ii = re.execute(str)
		for each i in ii
			x = ""
			k = lcase(i.subMatches(0))
			if inStr(k, " style") > 0 then
				re.pattern = "style\s*=""(.+?)"""
				set nn = re.execute(k)
				for each n in nn
					y = ""
					re.pattern = "(font-size|font-family|color|background-color)\s*:\s*([^;]+)"
					set mm = re.execute(k)
					for each m in mm
						y = y & m.subMatches(0) & ":" & replace(replace(m.subMatches(1), "\", ""), "expression", "") & ";"
					next
					if y <> "" then y = " style=""" & y & """"
					x = y
					exit for
				next
			end if
			y = noHtml(i.subMatches(1))
			if x <> "" then
				redim preserve data(j)
				data(j) = "<span" & x & ">" & y & "</span>"
				str = replace(str, i, tag1 & j & tag2)
				j = j + 1
			else
				str = replace(str, i, y)
			end if
		next
		
		'处理p
		re.pattern = "<p([^>]*)>([\S\s]*?)<\/\1>"
		set ii = re.execute(str)
		for each i in ii
			x = ""
			k = i.subMatches(0)
			if k <> "" then
				re.pattern = "align\s*=\s*""(center|right)"""
				set nn = re.execute(k)
				for each n in nn
					x = " align=""" & n.subMatches(0) & """"
					exit for
				next
			end if
			y = i.subMatches(1)
			if x <> "" then
				y = noHtml(y)
				redim preserve data(j)
				data(j) = "<p" & x & ">" & y & "</p>"
				str = replace(str, i, tag1 & j & tag2)
				j = j + 1
			else
				str = replace(str, i, y)
			end if
		next
		set nn = nothing
		set ii = nothing
		
		'处理多余的HTML
		str = noHtml(str)
		'还原标记的HTML代码
		j = j - 1
		do while j > -1
			str = replace(str, tag1 & j & tag2, data(j))
			j = j - 1
		loop
		str = replace(replace(str, tag1, ""), tag2, "")
		formatBook = str
	end function

	'过滤多余HTML代码
	public function noHtml(ByVal str)
		if trim(str) = "" then
			noHtml = ""
			exit function
		end if
		re.pattern = "<[^>]*>"
		str = re.replace(str, "")
		noHtml = str
	end function

	'ReBookUrl 还原内容里的A标签
	public function ReBookUrl(ByVal str)
		if len(trim(str)) < 1 then exit function
		dim i, ii
		re.pattern = "<span url=""([^""]+)"" class=""book_url cur"">([^<]+)<\/span>"
		set ii = re.execute(str)
		for each i in ii
			str = replace(str, i, "<a href=""" & i.subMatches(0) & """>" & i.subMatches(1) & "</a>")
		next
		set ii = nothing
		ReBookUrl = str
	end function

	'iMailTitle:标题
	'iMailBody:内容
	'iMailAddr:收信人地址
	'iMailSend:发信服务器
	'iMailDomain:发信域名
	'iMailUserName:登录用户名
	'iMailPass:登录密码
	'iMailFrom:发信人Email
	'iMailFromName:发信人名字
	public function sendEMail(iMailTitle, iMailBody, iMailAddr, iMailSend, iMailDomain, iMailUserName, iMailPassword, iMailFrom, iMailFromName)
		dim obj, str, ret, i, j
		str = ""
		if app.checkObject("JMail.SmtpMail") then
			j = split(iMailAddr, ";")
			set obj = server.createObject("JMail.message")
			obj.charset = "gb2312" '邮件编码
			obj.contentType = "text/html" '邮件正文格式
			obj.addHeader "mailer", "Microsoft Outlook Express 5.50.4807.1700"
			obj.Silent = true
			obj.Priority = 3 '邮件等级，1为加急，3为普通，5为低级
			obj.FromName = iMailFromName '发信人姓名
			obj.subject = iMailTitle '标题
			obj.Body = iMailBody '邮件正文(纯文本格式)
			'obj.HTMLBody = iMailBody '邮件正文(HTML格式)
			for i = 0 to ubound(j)
				obj.AddRecipient(j(i))
			next
			obj.mailServerUserName = iMailUserName '登录用户名
			obj.mailServerPassWord = iMailPassword '登录密码
			obj.mailDomain = iMailDomain '域名（如果用“name@domain.com”这样的用户名登录时，请指明domain.com
			obj.From = iMailFrom '发信人Email
			ret = obj.send(iMailSend)
			
			if len(obj.errormessage) > 0 then str = str & "<p>邮件发送过程中发生错误：" & obj.errormessage & "</p>"
			obj.close
			set obj = nothing
			if err.number <> 0 then str = str & "<p>邮件发送错误：" & err.description & "</p>"
			if ret then
				str = str & "<p>邮件发送成功！如果收不到邮件，请留言通知管理员。请登录邮箱查收！</p>"
			else
				str = str & "<p>邮件发送失败！请留言通知管理员。</p>"
			end if
		else
			str = "服务器没有安装邮件组件，所以无法发送邮件！"
		end if
		sendEMail = str
	end function
end class
%>